<?php
require_once("thamdinh/models/level.php");
class Danhmuc_TieuchiController extends GenericController {

    public function indexAction()
    {
        $this->_helper->layout->disableLayout();
        $params=$this->getRequest()->getParams();
        $config = Zend_Registry::get('config');
        if((int)$params['loai']>0)
        {
            if($params['loai']==1)
            {
                $data= Level::SelectLevelListByTYPE('TVTK_KHAC');
            }
            if($params['loai']==2)
            {
                $data= Level::SelectLevelListByTYPE('TVGS_KHAC');
            }
                
        }
        else
        {
            $data= Level::SelectLevelListByMultiTYPE(array('TVTK_KHAC','TVGS_KHAC'));
        } 
        if((int)$params['hienthi']>0)
        {
            $paginatorLimit=$params['hienthi'];
        }
        else 
        {
            $paginatorLimit=$config->paginatorLimit;
        }
        $paginatorRange=$config->paginatorRange;
        $paginator = Zend_Paginator::factory($data);
        $paginator->setItemCountPerPage($paginatorLimit);       
        $paginator->setPageRange($paginatorRange);
        $currentPage = $this->getRequest()->getParam('page',1);
        $paginator->setCurrentPageNumber($currentPage);        
        $this->view->data=$paginator;
        $this->view->limit=$paginatorLimit;
        $this->view->page=$currentPage;
        $this->view->params=$params;
        if($params['paginator']!=null)
        {
            $this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer('tieuchi/index', null, true);
        }
    }
    
    public function saveAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $param=$this->getRequest()->getParams();
        $id=$param['id'];
        if($param['LOAI']==1){$type="TVTK_KHAC";}
        if($param['LOAI']==2){$type="TVGS_KHAC";}
        $dataArray=array(
            'NAME'=>  trim($param['NAME']),
            'DIEM'=> (int) $param['DIEM'],
            'TYPE'=> $type
        );
        $model=new Level();
        if($id && (int)$id>0)
        {
            //UPDATE ACTION
            $model->update($dataArray,'ID_LV='.$id);
        }
        ELSE
        {
            //SAVE ACTION            
            $model->insert($dataArray);
        }
        $this->_redirect('/danhmuc/tieuchi/index');
    }
    
    public function inputAction()
    {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id && (int)$id>0)
        {
            $this->view->data= Level::SelectLevelByID($id);
        }        
    }
    public function deleteAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $ids=$this->getRequest()->getParam('ids');
        $model = new Level();
        foreach($ids as $id)
        {    
            for($i=0;$i<count($id);$i++)
            {
                $model->delete('ID_LV='.$id[$i]);
            }  
            
        }        
        $this->_redirect('/danhmuc/linhvuc/index');
    }
}
